<?php
class Pages extends Controller {

	function pages(){
		parent::Controller();	
	}
	
	function index()
	{
		$data['title']="God's Address";
		$data['content']='home';
		$this->load->view('template',$data,'');
	}
	function check_login()
	{
		$loged=$this->input->post('remember'); // If user wants to auto remember
		$username = $this->input->xss_clean($this->input->post('username'));    // EMAIL
	    $password = $this->input->post('password');
		$result=$this->user_model->check_login($username,$password);
		if($result->num_rows()>0)
		{
	       foreach ($result->result() as $row)
			{
					$_SESSION['username']=$username; // EMAIL
					$_SESSION['password']=$password;
					$_SESSION['user_id']=$row->user_id;
					$_SESSION['logged_in']=true;
					if ($loged=="yes")
					{
						// set cookie to REMEMBER
					}
			}
                redirect('/');
		}
		else
		{
	        //$this->session->set_flashdata('message', '<div id="message">Oopsie, Username or password is incorrect</div>');
	        //redirect('/');
			echo "hello";

		}
	}
	// ADD NEW USER
	function add_user()
	{
		$error='';
		$newUser = array();
		// VALIDATION before add
		// 1: check email if it is USED or NOT
		// 2: firstname, lastname ... is empty or not
		$newUser['first_name'] = $this->input->post('first_name', true);
		$newUser['last_name'] = $this->input->post('last_name', true);
        $newUser['email'] = $this->input->post('email');
		$newUser['password'] = $this->input->post('password');
		$newUser['country_id'] = $this->input->post('country');
		$newUser['state'] = $this->input->post('state', true);
        $newUser['city'] = $this->input->post('city', true);
		if($error=='')
		{
			if ($this->user_model->add_user($newUser))
				{
					$this->session->set_flashdata('message', 'SUCCESSFULLY');	    	
					redirect("/");
				}
				else
				{
					$this->session->set_flashdata('message', 'Error found');
					redirect("/");
				}
		}
	}
	// load the page tell a friend
	function tellafriend()
	{
		$data['title']="Tell a friend";
		$data['content']='tellafriend';
		$this->load->view('template',$data,'');
	}
	// sendmail to tell a friend about site
	function tellafriend_process()
	{
		$error='';		
		// Validation before send
		//..........
		//set the $error="Email is invalid"		...
		if($error=='')
		{
			if ($this->user_model->tellafriend_process($_POST))
			{
				$this->session->set_flashdata('message', 'Sent');	    	
				redirect("/");
			}
			else
			{
				$this->session->set_flashdata('message', 'Error');
				redirect("/");
			}
		}
	}
	// Contact
	function contact()
	{
		$data['title']="Contact";
		$data['content']='contact';
		$this->load->view('template',$data,'');
	}
	// sendmail to tell a friend about site
	function contact_process()
	{
		$error='';		
		// Validation before send
		//..........
		//set the $error="Email is invalid"		...
		if($error=='')
		{
			$message['name'] = $this->input->post('name', true);
			$message['familyname'] = $this->input->post('familyname', true);
			$message['email'] = $this->input->post('email', true);
			$message['subject'] = $this->input->post('subject', true);
			$message['description'] = $this->input->post('description', true);
			$mail = "dangkhoaweb@gmail.com";
			$this->user_model->sendEmail2($mail, 'Email from ..', 'contact',array('message'=>$message));
			$this->session->set_flashdata('message',"contact");
			redirect(base_url()."message.html");
		}
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */